Completed
Pull Request — master (#78)
by Ruben de
04:05 queued 01:39
created

client.createNewWallet   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
dl 0
loc 10
rs 9.4285
nop 3
1
var blocktrail = require('../'); // require('blocktrail-sdk') when trying example from in your own project
2
3
var client = blocktrail.BlocktrailSDK({
4
    apiKey : process.env.BLOCKTRAIL_SDK_APIKEY || "MY_APIKEY",
5
    apiSecret : process.env.BLOCKTRAIL_SDK_APISECRET || "MY_APISECRET",
6
    testnet : true
7
});
8
9
var ALLOW_ZERO_CONF = true;
10
11
var sendTransaction = function(wallet) {
12
    wallet.getNewAddress(function(err, address, path) {
13
        if (err) {
14
            return console.log("getNewAddress ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
15
        }
16
17
        console.log('new address', address, path);
18
19
        var pay = {};
20
        pay[address] = blocktrail.toSatoshi(0.001);
21
22
        wallet.pay(pay, null, ALLOW_ZERO_CONF, function(err, result) {
23
        // wallet.pay(pay, null, true, true, blocktrail.Wallet.FEE_STRATEGY_LOW_PRIORITY, function(err, result) {
24
            if (err) {
25
                return console.log("pay ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
26
            }
27
28
            console.log('transaction', result);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
29
        });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
30
    });
31
};
32
33
var action = 'default';
34
35
if (action === 'create') {
36
    client.createNewWallet({
37
        identifier: "example-wallet-sw",
38
        passphrase: "example-strong-password",
39
        keyIndex: 9999
40
    }, function(err, wallet, primaryMnemonic, backupMnemonic, blocktrailPubKeys) {
41
        if (err) {
42
            return console.log("createNewWallet ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
43
        }
44
45
        console.log('primary mnemonic', primaryMnemonic);
46
        console.log('backup mnemonic', backupMnemonic);
47
        console.log('blocktrail pubkeys', blocktrailPubKeys);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
48
    });
49
} else {
50
    client.initWallet({
51
        identifier: "example-wallet-sw",
52
        readOnly: true
53
    }, function(err, wallet) {
54
        if (err) {
55
            console.log('initWallet ERR', err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
56
            throw err;
57
        }
58
59
        wallet.getBalance(function(err, confirmed, unconfirmed) {
60
            if (err) {
61
                return console.log("getBalance ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
62
            }
63
64
            console.log('confirmed balance', confirmed);
65
            console.log('unconfirmed balance', unconfirmed);
66
67
            wallet.getNewAddress(function(err, address) {
68
                if (err) {
69
                    return console.log("getNewAddress ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
70
                }
71
72
                console.log('address', address);
73
74
                wallet.unlock({passphrase: "example-strong-password"}, function(err) {
75
                    if (err) {
76
                        return console.log("unlock ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
77
                    }
78
79
                    sendTransaction(wallet);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
80
                });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
81
            });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
82
        });
83
    });
84
}
85